<?php
/**
 * Created by PhpStorm.
 * User: Dingjinrong
 * Date: 16/9/26
 * Time: 下午3:39
 */
require_once "config.php";
require_once "funcs.php";

//function redPackageEntry($fromUsername, $toUsername, $conn, $weixin_nodes,
//                         $weixinPostObj)
//{
//    global $CONFIG;
//    $final_result = array('msg' => "欢迎使用" . $CONFIG['brand'].
//        "微信服务，您的输入暂时我们不能识别，请见谅\n", 'type' => 'text');
//<<<<<<< HEAD
//        $tempMsg = '泰禾一卡通领红包入口在<a href="http://test.lovemojito.com/weixin_didao/campaign_didao/bind/index.php">这里</a>';
//=======
//        $tempMsg = '活动入口在<a href="http://test.lovemojito.com/weixin_didao/campaign_didao/bind/index.php">这里</a>';
//>>>>>>> 38555d444cbd5685e84394428eda3913f01c2a74
//    $final_result['msg'] = responseText($fromUsername, $toUsername, $tempMsg);
//
//    return $final_result;
//}
//
//
function sendRedPackage($fromUsername, $toUsername, $conn, $weixin_nodes,
                        $weixinPostObj)
{
    global $CONFIG;
    $final_result = array('msg' => "欢迎使用" . $CONFIG['brand'] .
        "微信服务，您的输入暂时我们不能识别，请见谅\n", 'type' => 'text');
    $tempMsg = '活动已过期';
//    $fromUsername = trim($fromUsername);
//    $sql = "SELECT * FROM TH_redPacket_withdraw_log_crd where openid = '$fromUsername'";
//    $conn = connect_to_db();
//    $ret = mysql_query($sql,$conn);
//    $tempMsg= '';
//    if(is_resource($ret)&&mysql_num_rows($ret)){
//        $tempMsg = '您已经领取过咯,只能领取一次哦';
//    }else{
//        $openid = $fromUsername;
//        $money = 8.8;
//            $withdrawID = md5($openid . $money . time() . random_string(6));
//            $sql = "insert into `TH_redPacket_withdraw_log_crd` (`openid`, `money`, `withdrawStatus`, `withdrawID`, updateTime) values";
//            $sql .= "('$openid', $money, 'pending', '$withdrawID', now());";
//            mysql_query($sql, $conn);
//            //下面开始调用接口提现
//
//            $moneyYuan = $money * 100; //先设置默认金额 分为单位
////                $money = 100; //先设置默认金额
//            $key = 'didaoolamk5AB24giest5weixinlhlCC@#fKM';
//            $unixtime = time();
//            $token = md5($openid . $key . $unixtime);
//            $url = "http://app.lovemojito.com/didao/wxpay/api/transfer.php?openid=$openid&token=$token&unixtime=$unixtime&amount=$moneyYuan&orderid=$withdrawID&module=taihe";
//            $data = fetchUrlContent($url); //通过自定义函数getCurl得到https的内容
//            $transferResult = json_decode($data, true); //转为数组
//
//        //提现成功则更新数据库并返回
//        if (isset($transferResult['result_code']) && $transferResult['result_code'] == 'SUCCESS'  )
//        {
//            $payment_no = mysql_real_escape_string($transferResult['payment_no']);
//            $payment_time = mysql_real_escape_string($transferResult['payment_time']);
//            $sql = "update TH_redPacket_withdraw_log_crd set withdrawStatus = 'success', payment_time = '$payment_time', payment_no = '$payment_no' where withdrawID = '$withdrawID' and openid = '$openid' limit 1;";
//            mysql_query($sql, $conn);
//            $errorno = 0;
//            $tempMsg = "微策传媒 陈荣第 13466763011 \n提前预祝双旦快乐!\n请查收微信零钱。";
//        } else
//        {
//            $errorno = -1;
//            $tempMsg = '因微信接口限制，该次提现未成功，后台将持续为您提现，请稍候查看微信零钱账户';
//        }
//
//
//    }

    $final_result['msg'] = responseText($fromUsername, $toUsername, $tempMsg);

    return $final_result;
}

function didaoPayNeedFollow($fromUsername, $toUsername, $conn, $weixin_nodes,
                            $weixinPostObj)
{
    global $CONFIG;
    $fromUsername = trim($fromUsername);
    $final_result = array('msg' => "欢迎使用" . $CONFIG['brand'] .
        "微信服务，您的输入暂时我们不能识别，请见谅\n", 'type' => 'text');
    //获取该openid的pay账单号,拼接到 needpay后
    $sql = "SELECT order_id FROM weixin_pay_order where openid = '$fromUsername'ORDER BY order_createtime DESC LIMIT 1";
    $ret = mysql_query($sql, $conn);
    $order_id = '00000';
    if (is_resource($ret) and mysql_num_rows($ret)) {
        $row = mysql_fetch_array($ret);
        $order_id = $row[0];
    }
    $url = $CONFIG['UrlPrefix'] . "campaign_didao/pay/needPay.php?order_id=" . $order_id;
    $tempMsg = "感谢您关注帝道优品\n";
    $tempMsg .= '请点击链接<a href="'.$url.'">这里</a>继续完成订单支付吧';
    $final_result['msg'] = responseText($fromUsername, $toUsername, $tempMsg);

    return $final_result;
}
function scanTest($fromUsername, $toUsername, $conn, $weixin_nodes,
                            $weixinPostObj)
{
    global $CONFIG;
    $fromUsername = trim($fromUsername);
    $final_result = array('msg' => "欢迎使用" . $CONFIG['brand'] .
        "微信服务，您的输入暂时我们不能识别，请见谅\n", 'type' => 'text');
    //获取该openid的pay账单号,拼接到 needpay后
    $sql = "SELECT comment FROM weixin_nickname where fromUsername = '$fromUsername' LIMIT 1";
    $ret = mysql_query($sql, $conn);
    $comment = '' ;

    if (is_resource($ret) and mysql_num_rows($ret)) {
        $row = mysql_fetch_array($ret);
        $comment = $row[0];
    }
        $comment = $comment==''?"self":$comment;
    $url = $CONFIG['UrlPrefix'] . "sportMeet/index.php?srcopenid=DiDao_DT_".$comment;
    $tempMsg  = "感谢您关注帝道优品\n";
    $tempMsg .= "《亲子运动会》\n".'<a href="'.$url.'">【报名地址赶快报名吧】</a> !';
    $final_result['msg'] = responseText($fromUsername, $toUsername, $tempMsg);

    return $final_result;
}


?>